//
//  news.m
//  EFExtend
//
//  Created by mac on 15/3/12.
//  Copyright (c) 2015年 Eric. All rights reserved.
//

#import "news.h"


@interface news ()<HMUIPhotoBrowserDelegate,HMUIPhotoBrowserDatasource>
@property(nonatomic,strong)NSMutableArray *imagelist;
@property(nonatomic,strong)HMUIPhotoBrowser *newsView;

@end

@implementation news{
    NSInteger pn;
    NSInteger rn;
}
@synthesize imagelist;

- (void)dealloc
{
    self.newsView = nil;
    self.imagelist = nil;
    HM_SUPER_DEALLOC();
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self.customNavLeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavLeftBtn setFrame:CGRectMakeBound(32, 32)];

    [self.customNavRightBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavRightBtn setFrame:CGRectMakeBound(32, 32)];

    
    self.imagelist = [NSMutableArray arrayWithArray:@[@{},@{}]];
    
    self.newsView = [HMUIPhotoBrowser spawn];
//    self.newsView.frame = CGRectMake(0, 0, self.view.width, 200);
//    self.newsView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin;
    self.newsView.delegate= self;
    self.newsView.dataSource = self;
    self.newsView.allowZoom = NO;
    self.newsView.allowAutoScroll = YES;
//    self.newsView.circulation = NO;
    self.newsView.showText = YES;
    [self.view addSubview:self.newsView];
    [self.newsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
    }];
//    rn = 15;
//    [self getwallmore];
}

- (void)viewDidAppear:(BOOL)animated{
    [self.newsView reloadData];
}

- (void)showbrowser:(NSInteger)cuttent{
    HMUIPhotoBrowser *browserboard = [HMUIPhotoBrowser spawn];
    
//    NSMutableArray *arry = [NSMutableArray array];
//    for (NSDictionary *detail in imagelist) {
//        HMPhotoItem *item = [[HMPhotoItem alloc]init];
//        
//        item.image = [UIImage imageNamed:@"main_mainbar_bg.png"];
//        item.webUrl = [detail valueForKey:@"download_url"];
//        [arry addObject:item];
//    }
    browserboard.allowZoom = YES;
    browserboard.allowOriententation = NO;
    browserboard.currentPhotoIndex = cuttent;
    browserboard.tag = 120;
    browserboard.delegate = self;
    browserboard.dataSource = self;
//    browserboard.photos = arry;
    HMPhotoCell *cell = [self.newsView currentPhotoView];
    [browserboard showFromView:cell forImage:cell.imageView.image];
}
- (HMPhotoItem *)photoBrowser:(HMUIPhotoBrowser *)browser itemAtIndex:(NSUInteger)index{
    NSDictionary *detail = [imagelist safeObjectAtIndex:index];
    HMPhotoItem *item = [[HMPhotoItem alloc]init];
    
    item.placeholder = [UIImage imageNamed:@"nav_bg.png"];
    item.webUrl = [detail valueForKey:@"download_url"];

    return item;
}

- (NSUInteger)photoBrowserNumbers:(HMUIPhotoBrowser *)browser{
    return imagelist.count;
}

-   (void)photoBrowser:(HMUIPhotoBrowser *)photoBrowser didChangedToPageAtIndex:(NSUInteger)index{
    
}

- (BOOL)photoBrowser:(HMUIPhotoBrowser *)photoBrowser didTouchedIndex:(NSUInteger)index{
    if (photoBrowser==self.newsView) {
        [self showbrowser:index];
        return YES;
    }
    return NO;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

ON_Button(signal){
    UIButten *btn = signal.source;
    if ([signal is:[UIButten TOUCH_UP_INSIDE]]) {
        if ([btn is:@"leftBtn"]) {//customNavLeftBtn
            [self backAndRemoveWithAnimate:YES];
        }else if ([btn is:@"rightBtn"]){//customNavRightBtn
            rn = 1;
            pn = 0;
            [self getwallmore];
        }
    }
}
- (void)getwallmore{
    
    NSDictionary *para = @{@"pn":@(pn),
                           @"rn":@(rn),
                           @"tag1":@"美女",
                           @"tag2":@"清纯",
                           @"ie":@"utf8"};
    [[[[self webApiUniqueCommand:@"http://image.baidu.com/channel/listjson" name:@"getImage" timeout:30.f]
       setWebParams:para] setWebCacheDuration:0]
     send];
}


ON_WebAPI(dlg){
    
    if (dlg.sending) {
        
        
    }else if (dlg.succeed){
        NSDictionary *dic = [dlg.output objectForKey:[HMWebAPI params]];
        if (self.imagelist==nil) {
            self.imagelist = [NSMutableArray array];
        }
        if (pn==0) {
            [self.imagelist removeAllObjects];
        }
        [self.imagelist addObjectsFromArray:[dic objectForKey:@"data"]];
        if ([[self.imagelist lastObject] allKeys].count==0) {
            [self.imagelist removeLastObject];
        }
        
        pn+=rn;
        
        NSMutableArray *arry = [NSMutableArray array];
        NSInteger count =0;
        for (NSDictionary *detail in imagelist) {
            HMPhotoItem *item = [[HMPhotoItem alloc]init];
            
            item.image = [UIImage imageNamed:@"back.png"];
            item.webUrl = [detail valueForKey:@"image_url"];
            [arry addObject:item];
        }
        [self.newsView reloadData];
        
    }else if (dlg.failed){
        
        if (dlg.timeout) {
            [self showFailureTip:@"操作失败" detail:@"链接超时" timeOut:3.f];
            return;
        }
        
        [self showFailureTip:@"网络链接失败" detail:@"数据不存在或网络错误" timeOut:3.f];
    }else if (dlg.cancelled){
        
    }
    //#if (__ON__ == __HM_DEVELOPMENT__)
    //    CC( [self.class description],dlg.responseData?dlg.responseData:dlg.errorDesc);
    //#endif	// #if (__ON__ == __BEE_DEVELOPMENT__)
}
@end
